﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.Configuration;

namespace WebSite
{
    public partial class Default : System.Web.UI.Page
    {
        private string _seperator = ",";
        private Dictionary<string, string> Configs;
        protected void Page_Load(object sender, EventArgs e)
        {
            Configs = new Dictionary<string, string>();
            this.ReadRequestSettings();
            this.ReadWebConfigAppSettings();
            WriteInitParams();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }
        private void ReadRequestSettings()
        {
            foreach (string key in Request.QueryString.AllKeys)
            {
                if (Configs.ContainsKey(key) == false)
                    Configs.Add(key, Request.QueryString[key]);
            }
        }
        private void ReadWebConfigAppSettings()
        {
            NameValueCollection appSettings = WebConfigurationManager.AppSettings;

            foreach (string key in appSettings.AllKeys)
            {
                if (Configs.ContainsKey(key) == false)
                    Configs.Add(key, appSettings[key]);
            }
        }

        private void WriteInitParams()
        {
            NameValueCollection appSettings = WebConfigurationManager.AppSettings;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("<param name=\"InitParams\" value=\"");
            string paramContent = string.Empty;
            foreach (KeyValuePair<string, string> entity in Configs)
            {
                paramContent += string.Format("{0}={1}{2}", entity.Key, entity.Value, _seperator);
            }
            stringBuilder.Append(paramContent);
            stringBuilder.Append("\" />");
            this.litInitParams.Text = stringBuilder.ToString();
        }
    }
}